-- print("[BAREBONES] barebones.lua")

-- Generated from template
if GameMode == nil then
    --	print ( '[BAREBONES] creating barebones game mode' )
    GameMode = class({})
end

-- This function initializes the game mode and is called before anyone loads into the game
-- It can be used to pre-initialize any values/tables that will be needed later
function GameMode:InitGameMode()
    print( "Template addon is loaded." )
	
    
    GameRules:SetCustomGameTeamMaxPlayers(DOTA_TEAM_GOODGUYS, TEAM_SIZE)
    GameRules:SetCustomGameTeamMaxPlayers(DOTA_TEAM_BADGUYS, TEAM_SIZE)
    GameRules:SetHeroRespawnEnabled(ENABLE_HERO_RESPAWN)
    GameRules:SetUseUniversalShopMode(UNIVERSAL_SHOP_MODE)
    GameRules:SetSameHeroSelectionEnabled(ALLOW_SAME_HERO_SELECTION)
    GameRules:SetHeroSelectionTime(HERO_SELECTION_TIME)
    GameRules:SetPreGameTime(PRE_GAME_TIME)
    GameRules:SetPostGameTime(POST_GAME_TIME)
    GameRules:SetTreeRegrowTime(TREE_REGROW_TIME)
    GameRules:SetUseCustomHeroXPValues(USE_CUSTOM_XP_VALUES)
    GameRules:SetGoldPerTick(GOLD_PER_TICK)
    GameRules:SetGoldTickTime(GOLD_TICK_TIME)
    GameRules:SetRuneSpawnTime(RUNE_SPAWN_TIME)
    GameRules:SetUseBaseGoldBountyOnHeroes(USE_STANDARD_HERO_GOLD_BOUNTY)
    GameRules:SetHeroMinimapIconScale(MINIMAP_ICON_SIZE)
    GameRules:SetCreepMinimapIconScale(MINIMAP_CREEP_ICON_SIZE)
    GameRules:SetRuneMinimapIconScale(MINIMAP_RUNE_ICON_SIZE)

    -- print("[BAREBONES] GameRules set")

    -- Listeners - Event Hooks
    -- All of these events can potentially be fired by the game, though only the uncommented ones have had
    -- Functions supplied for them.
    ListenToGameEvent("dota_player_gained_level", Dynamic_Wrap(GameMode, "OnPlayerLevelUp"), self)
    ListenToGameEvent("dota_ability_channel_finished", Dynamic_Wrap(GameMode, "OnAbilityChannelFinished"), self)
    ListenToGameEvent("dota_player_learned_ability", Dynamic_Wrap(GameMode, "OnPlayerLearnedAbility"), self)
    ListenToGameEvent("entity_killed", Dynamic_Wrap(GameMode, "OnEntityKilled"), self)
    ListenToGameEvent("player_connect_full", Dynamic_Wrap(GameMode, "OnConnectFull"), self)
    ListenToGameEvent("player_disconnect", Dynamic_Wrap(GameMode, "OnDisconnect"), self)--玩家离开
    ListenToGameEvent("dota_item_purchased", Dynamic_Wrap(GameMode, "OnItemPurchased"), self)--购买DOTA物品
    ListenToGameEvent("dota_item_picked_up", Dynamic_Wrap(GameMode, "OnItemPickedUp"), self)--捡东西
    ListenToGameEvent("last_hit", Dynamic_Wrap(GameMode, "OnLastHit"), self)
    ListenToGameEvent("dota_non_player_used_ability", Dynamic_Wrap(GameMode, "OnNonPlayerUsedAbility"), self)
    ListenToGameEvent("player_changename", Dynamic_Wrap(GameMode, "OnPlayerChangedName"), self)
    ListenToGameEvent("dota_rune_activated_server", Dynamic_Wrap(GameMode, "OnRuneActivated"), self)
    ListenToGameEvent("dota_player_take_tower_damage", Dynamic_Wrap(GameMode, "OnPlayerTakeTowerDamage"), self)
    ListenToGameEvent("tree_cut", Dynamic_Wrap(GameMode, "OnTreeCut"), self)
    ListenToGameEvent("entity_hurt", Dynamic_Wrap(GameMode, "OnEntityHurt"), self)
    ListenToGameEvent("player_connect", Dynamic_Wrap(GameMode, "PlayerConnect"), self)--玩家最初连接但未完全连接时，此功能被调用1至2次
    ListenToGameEvent("dota_player_used_ability", Dynamic_Wrap(GameMode, "OnAbilityUsed"), self)
    ListenToGameEvent("game_rules_state_change", Dynamic_Wrap(GameMode, "OnGameRulesStateChange"), self)
    ListenToGameEvent("npc_spawned", Dynamic_Wrap(GameMode, "OnNPCSpawned"), self)
    ListenToGameEvent("dota_player_pick_hero", Dynamic_Wrap(GameMode, "OnPlayerPickHero"), self)
    ListenToGameEvent("dota_team_kill_credit", Dynamic_Wrap(GameMode, "OnTeamKillCredit"), self)
    ListenToGameEvent("player_reconnected", Dynamic_Wrap(GameMode, "OnPlayerReconnect"), self)--重连
    ListenToGameEvent("player_chat", Dynamic_Wrap(CommandEngine, "CheckCommand"), self)

    -- Change random seed
    local timeTxt = string.gsub(string.gsub(GetSystemTime(), ":", ""), "0", "")
    math.randomseed(tonumber(timeTxt))

end

mode = nil
-- This function is called once when the player fully connects and becomes "Ready" during Loading
function GameMode:OnConnectFull(keys)
    -- print("[BAREBONES] OnConnectFull")

    if  mode == nil then
        -- Set GameMode parameters
        mode = GameRules:GetGameModeEntity()
    
        --mode:SetLoseGoldOnDeath(false)--死亡后自己不扣钱
        mode:SetTopBarTeamValuesVisible( true )
        mode:SetStashPurchasingDisabled( true )
        mode:SetFogOfWarDisabled( true )
        mode:SetCameraDistanceOverride( 1500 )
        mode:SetCustomGameForceHero(SET_FORCE_HERO)--指定英雄，跳过选人和准备
        -- Disable runes
        for rune = 1, 12, 1 do
            mode:SetRuneEnabled(rune, false)
        end
        mode:SetRecommendedItemsDisabled(RECOMMENDED_BUILDS_DISABLED)
        mode:SetCameraDistanceOverride(CAMERA_DISTANCE_OVERRIDE)
        mode:SetCustomBuybackCostEnabled(CUSTOM_BUYBACK_COST_ENABLED)
        mode:SetCustomBuybackCooldownEnabled(CUSTOM_BUYBACK_COOLDOWN_ENABLED)
        mode:SetBuybackEnabled(BUYBACK_ENABLED)
        mode:SetTopBarTeamValuesOverride(USE_CUSTOM_TOP_BAR_VALUES)
        mode:SetTopBarTeamValuesVisible(TOP_BAR_VISIBLE)
        mode:SetUseCustomHeroLevels(USE_CUSTOM_HERO_LEVELS)
        mode:SetCustomHeroMaxLevel(MAX_LEVEL)
        mode:SetCustomXPRequiredToReachNextLevel(XP_PER_LEVEL_TABLE)

        --mode:SetBotThinkingEnabled( USE_STANDARD_DOTA_BOT_THINKING )
        mode:SetTowerBackdoorProtectionEnabled(ENABLE_TOWER_BACKDOOR_PROTECTION)

        mode:SetFogOfWarDisabled(DISABLE_FOG_OF_WAR_ENTIRELY)
        mode:SetGoldSoundDisabled(DISABLE_GOLD_SOUNDS)
        mode:SetRemoveIllusionsOnDeath(REMOVE_ILLUSIONS_ON_DEATH)

    end
end

-- This is an example console command
function GameMode:ExampleConsoleCommand()
    -- print("******* Example Console Command ***************")
    local cmdPlayer = Convars:GetCommandClient()
    if cmdPlayer then
        local playerID = cmdPlayer:GetPlayerID()
        if playerID ~= nil and playerID ~= -1 then
            -- Do something here for the player who called this command
            PlayerResource:ReplaceHeroWith(playerID, "npc_dota_hero_viper", 1000, 1000)
        end
    end
    -- print("*********************************************")
end

-

--[[
This function is called once and only once for every player when they spawn into the game for the first time.  It is also called
if the player's hero is replaced with a new hero for any reason.  This function is useful for initializing heroes, such as adding
levels, changing the starting gold, removing/adding abilities, adding physics, etc.

The hero parameter is the hero entity that just spawned in.
]]
function GameMode:OnHeroInGame(hero)
    -- print("[BAREBONES] Hero spawned in game for first time -- " .. hero:GetUnitName())

    -- Store a reference to the player handle inside this hero handle.
    hero.player = PlayerResource:GetPlayer(hero:GetPlayerID())
    -- Store the player's name inside this hero handle.
    hero.playerName = PlayerResource:GetPlayerName(hero:GetPlayerID())

    local innate_ability = hero:FindAbilityByName("builder_invulnerable")
    if innate_ability then
        innate_ability:SetLevel(1)
        innate_ability:SetHidden(true)
    end
end


-- The overall game state has changed
function GameMode:OnGameRulesStateChange(keys)
    -- print("[BAREBONES] GameRules State Changed")

end

-- An entity somewhere has been hurt.  This event fires very often with many units so don't do too many expensive
-- operations here
function GameMode:OnEntityHurt(keys)
    ---- print("[BAREBONES] Entity Hurt")
    ---- DeepPrintTable(keys)
    if keys.entindex_attacker then
        local entCause = EntIndexToHScript(keys.entindex_attacker)
    end
    if keys.entindex_killed then
        local entVictim = EntIndexToHScript(keys.entindex_killed)
    end
end

